<!--
 * @description: 电气 自动算法调用
 * @author: cuicui
 * @Date: 2023-11-23 11:55:06
 * @LastEditTime: 2023-11-30 16:06:27
 * @LastEditors: cuicui
-->
<template>
  <business-layout title="自动算法调用">
    <template slot="query">
      <a-row style="height: 40px;">
        <a-col :span="22">
          <a-form :form="form1" layout="inline" @submit="handleQueryData">
            <a-form-item>
              <a-select style="width: 300px;" placeholder="请选择设备" v-decorator="['equipment_type']">
                <a-select-option value="0">类型1</a-select-option>
                <a-select-option value="1">类型2</a-select-option>
                <a-select-option value="2">类型3</a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item>
              <a-button type="primary" html-type="submit">
                <a-icon type="search" />
                查询
              </a-button>
            </a-form-item>
          </a-form>
        </a-col>
      </a-row>
    </template>

    <template slot="btns">
      <div>
        <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button>
        <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item key="1"><a-icon type="delete" />删除</a-menu-item>
            <!-- lock | unlock -->
            <a-menu-item key="2"><a-icon type="lock" />锁定</a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px">
            批量操作 <a-icon type="down" />
          </a-button>
        </a-dropdown>
      </div>
    </template>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      showPagination='auto'
    >
<!--      :alert="true"-->
<!--      :rowSelection="rowSelection"-->
<!--      showPagination="auto"-->
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
      <span slot="description" slot-scope="text">
        <ellipsis :length="4" tooltip>{{ text }}</ellipsis>
      </span>

      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleRun(record)">运行</a>
          <a-divider type="vertical" />
          <a @click="handleUpdate(record)">编辑</a>
          <a-divider type="vertical" />
          <a class='del-link' @click="handleRemove(record)">删除</a>
        </template>
      </span>
    </s-table>

    <create-form
      ref="createModal"
      :visible="visible"
      :loading="confirmLoading"
      :model="mdl"
      @cancel="handleCancel"
      @ok="handleOk"
    />
  </business-layout>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'

import CreateForm from '@/business-components/create-task'

const columns = [
  {
    title: '#',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '任务名称',
    dataIndex: 'description',
    scopedSlots: { customRender: 'description' }
  },
  {
    title: '任务说明',
    dataIndex: 'id1'
  },
  {
    title: '上次执行时间',
    dataIndex: 'point_name'
  },
  {
    title: '下次执行时间',
    dataIndex: 'updateDate'
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '10%',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    CreateForm
  },
  data () {
    this.columns = columns
    return {
      form1: this.$form.createForm(this),
      activeKey: '1',
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        console.log('loadData request parameters:', requestParameters)
        return new Promise((resolve, reject) => {
          resolve({
            endRow: 10,
            hasNextPage: false,
            hasPreviousPage: false,
            isFirstPage: false,
            isLastPage: false,
            list: [{}],
            navigateFirstPage: 0,
            navigateLastPage: 0,
            navigatePages: 0,
            navigatepageNums: null,
            nextPage: 0,
            pageNum: 1,
            pageSize: 10,
            pages: 3,
            prePage: 0,
            size: 10,
            startRow: 0,
            total: 1
          })
        })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
  },

  computed: {
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    handleQueryData () {},
    handleAdd () {
      this.mdl = null
      this.visible = true
    },
    handleEdit (record) {
      this.visible = true
      this.mdl = { ...record }
    },
    handleOk () {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('values', values)
          if (values.id > 0) {
            // 修改 e.g.
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('更新成功')
            })
          } else {
            // 新增
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('新增成功')
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false

      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleRun (record) {
        this.$message.success(`${record.no} 运行成功`)
    },
    handleUpdate (record) {
        this.$message.success(`${record.no} 更新成功`)
    },
    handleRemove (record) {
        this.$message.success(`${record.no} 删除成功`)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
